//
//  AppDelegate.swift
//  SpeechAnalyzerDemo
//
//  改为使用 UIKit 生命周期，替换原有 SwiftUI 入口。
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    private let sharedManager = SpeechAnalyzeManager()

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        // iOS 12 及以下没有 SceneDelegate，需要手动创建窗口。
        if #available(iOS 13.0, *) {
            return true
        } else {
            let window = UIWindow(frame: UIScreen.main.bounds)
            let navController = UINavigationController(rootViewController: ContentViewController(manager: sharedManager))
            window.rootViewController = navController
            window.makeKeyAndVisible()
            self.window = window
            return true
        }
    }

    @available(iOS 13.0, *)
    func application(
        _ application: UIApplication,
        configurationForConnecting connectingSceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        let configuration = UISceneConfiguration(name: "默认配置", sessionRole: connectingSceneSession.role)
        configuration.delegateClass = SceneDelegate.self
        return configuration
    }
}

@available(iOS 13.0, *)
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    private let manager = SpeechAnalyzeManager()

    func scene(
        _ scene: UIScene,
        willConnectTo session: UISceneSession,
        options connectionOptions: UIScene.ConnectionOptions
    ) {
        guard let windowScene = scene as? UIWindowScene else { return }

        let window = UIWindow(windowScene: windowScene)
        let navigationController = UINavigationController(rootViewController: ContentViewController(manager: manager))
        window.rootViewController = navigationController
        window.makeKeyAndVisible()
        self.window = window
    }
}
